package com.example.a97369.zyfupt_wkm306.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.a97369.zyfupt_wkm306.R;
import com.example.a97369.zyfupt_wkm306.bean.LoginBean;
import com.example.a97369.zyfupt_wkm306.iface.LoginListener;
import com.example.a97369.zyfupt_wkm306.model.LoginModel;

/**
 * Created by 97369 on 2017/5/8.
 */

public class    LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Button loginButton, registerButton;
    private EditText idET, pwdEt;
    private String username="",password="",sessionID="";//用户名密码
    private SharedPreferences sp;
    private Switch sw;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getId()!=null){
                sessionID=loginBean.getSessionid().toString();
                System.out.print("===sessionID"+sessionID);
                savaSp();
                Intent intent1 = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent1);
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"对不起登陆失败",Toast.LENGTH_SHORT).show();
        }
    };

    private void savaSp() {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionID",sessionID);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();
    }


    private void successToNext(){
        Intent intent2 = new Intent();
        intent2.setClass(this, MainActivity.class);//forward为当前activity，forwardTarget为新的activity
        startActivity(intent2);
        finish();
    }



    private void init() {
        idET = (EditText)findViewById(R.id.editText_login_username);
        pwdEt = (EditText)findViewById(R.id.editText_login_password);
        loginButton=(Button)findViewById(R.id.button_login);
        registerButton=(Button)findViewById(R.id.button_login_reg);

        sw=(Switch)findViewById(R.id.switch1);
        loginButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b = sp.getBoolean("remember",false);
        System.out.print("===name"+name);
        if(b){
            idET.setText(name);
            pwdEt.setText(pass);
            sw.setChecked(b);
        }
    }

    public void login(View view){
        LoginModel loginModel = new LoginModel();
        loginModel.getResult(idET.getText().toString(), pwdEt.getText().toString(), loginListener);
    }
    public void register(View view){
        Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.button_login:
                System.out.println("点击登陆按钮");
                LoginModel loginModel = new LoginModel();
                username=idET.getText().toString();
                password=pwdEt.getText().toString();
                loginModel.getResult(username, password, loginListener);

                break;
            case R.id.button_login_reg:
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);//forward为当前activity，forwardTarget为新的activity
                startActivity(intent);
                break;
        }
    }
}

